package com.kits.store.ui

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kits.store.R
import com.kits.store.core.InnerStorage
import com.kits.store.core.Utils
import kotlinx.android.synthetic.main.fragment_inner_storage.*
import me.yokeyword.fragmentation.SupportFragment
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream

/**
 * 内部存储的使用不需要申请权限
 */
class InnerStorageFragment:SupportFragment(){
    companion object{
        fun newInstance(): InnerStorageFragment{
            val args = Bundle()

            val fragment = InnerStorageFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_inner_storage, container, false)
    }

    override fun onResume() {
        super.onResume()
        initView()
    }

    private fun initView(){
        btnWriteFile.setOnClickListener {
            val file = InnerStorage.file(requireContext(),null,"justFile1029.txt")
            println("file 绝对路径 ${file?.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向文件justFile1029.txt中写入数据，该数据可以存放比较大的数据\n".toByteArray())
            outputStream.close()
        }

        btnReadFile.setOnClickListener {
            val file = InnerStorage.file(requireContext(),null,"justFile1029.txt")
            val inputStream = FileInputStream(file)
            val byteBuf = ByteArray(inputStream.available())
            inputStream.read(byteBuf)
            val strBuf = String(byteBuf)
            println("strBuf == $strBuf")

        }

        btnWriteCacheFile.setOnClickListener {
            cacheWriteFile()
        }
        btnReadCacheFile.setOnClickListener {
            cacheReadFile()
        }

        btnFileStream.setOnClickListener {
            val file = InnerStorage.fileStreamPath(requireContext(),"/aa.txt")
            println("file == ${file.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向FileStream中写入数据，该数据可以存放比较大的数据\n".toByteArray())
            outputStream.close()
        }

        btnFileList.setOnClickListener {
            val list = InnerStorage.fileList(requireContext())
            list.forEach {
                println("it == $it")
            }
        }

        btnWriteDataFile.setOnClickListener {
            val file = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                InnerStorage.dataFile(requireContext(),"testData","DataFile1029.txt")
            } else {
                null
            }
            println("file 绝对路径 ${file?.absolutePath}")
            val outputStream = FileOutputStream(file)
            outputStream.write("向文件 DataFile1029.txt中写入数据，该数据可以存放比较大的数据\n".toByteArray())
            outputStream.close()
        }

        btnReadDataFile.setOnClickListener {
            val file = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                InnerStorage.dataFile(requireContext(),"testData","DataFile1029.txt")
            } else {
                null
            }
            file?.let {
                val inputStream = FileInputStream(file)
                val byteBuf = ByteArray(inputStream.available())
                inputStream.read(byteBuf)
                val strBuf = String(byteBuf)
                println("strBuf == $strBuf")
            }

        }

        btnOutputFile.setOnClickListener {
            val outputStream = InnerStorage.outputFile(requireContext(),"Simple1029PM.txt",Context.MODE_WORLD_READABLE)
            val bytes = "向Simple1029PM 写入数据 \n".toByteArray(Charsets.UTF_8)
            outputStream.write(bytes)
            outputStream.close()
        }

        btnInputFile.setOnClickListener {
            val inputStream = InnerStorage.inputFile(requireContext(),"Simple1029PM.txt")
            val byteBuf = ByteArray(inputStream.available())
            inputStream.read(byteBuf)
            val strBuf = String(byteBuf,Charsets.UTF_8)
            println("strBuf == $strBuf")
        }

        btnObbFiles.setOnClickListener {
            val fileList = InnerStorage.obbFileList(requireContext())
            fileList.forEach {
                println("Path == ${it.absolutePath}")
            }

            val wFile = InnerStorage.obbDirFile(requireContext(),"test","myTest.txt")
            wFile?.let {
                println("file 绝对路径 ${it.absolutePath}")
                val outputStream = FileOutputStream(it)
                outputStream.write("写入数据----------\n".toByteArray())
                outputStream.close()
            }

            val rFile = InnerStorage.obbDirFile(requireContext(),"test","myTest.txt")
            rFile?.let {
                val inputStream = FileInputStream(it)
                val byteBuf = ByteArray(inputStream.available())
                inputStream.read(byteBuf)
                val strBuf = String(byteBuf)
                println("strBuf == $strBuf")
            }
        }

    }

    private fun cacheWriteFile(){
        val cache = InnerStorage.cachesFile(requireContext(),"/cacheTest","myCache.txt")
        println("file 绝对路径 ${cache?.absolutePath}")
        val outputStream = FileOutputStream(cache)
        outputStream.write("向缓存文件中写入数据，数据文件最大为1M，超过系统会清空\n".toByteArray())
        outputStream.close()
    }

    private fun cacheReadFile(){
        val cache = InnerStorage.cachesFile(requireContext(),"/cacheTest","myCache.txt")
        val inputStream = FileInputStream(cache)
        val byteBuf = ByteArray(inputStream.available())
        inputStream.read(byteBuf)
        val strBuf = String(byteBuf)
        println("strBuf == $strBuf")
    }

}